#include <algorithm>
#include <cmath>
#include <cstdio>

const int N = 400010;
int n, m;
int a[N], f[N][20];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
#endif
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		f[i][0] = a[i];
	}
	for (int j = 1; j < 18; ++j) {
		for (int i = 1; i <= n; ++i) {
			f[i][j] = std::max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
		}
	}
	int l, r;
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &l, &r);
		int k = std::log(r - l + 1) / std::log(2);
		printf("%d\n", std::max(f[l][k], f[r - (1 << k) + 1][k]));
	}
	return 0;
}